# ！/usr/bin/env python
# -*-coding:utf-8-*-
"""
@Author  : zhangfujie
@Time    : 2020-06-13 15:16:06
"""
import sys, os
import you_get
import threading
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class ZFJVideoView(QMainWindow):
	"""docstring for ZFJVideoView"""
	def __init__(self):
		super().__init__()
		self.init_data()
		self.ui_config()
		threading.Timer(2, self.show_message, ()).start()

	def show_message(self):
		pass

	def init_data(self):
		self.translate = QtCore.QCoreApplication.translate
		self.is_downloading = False

		self.width = 600
		self.height = 420
		self.margin_size = 10

		self.setWindowFlags(QtCore.Qt.WindowTitleHint|QtCore.Qt.WindowCloseButtonHint)
		self.setStyleSheet("background-color:#fff")
		self.setWindowTitle('ZFJVideoTool-作者QQ：2723834718')
		self.resize(self.width, self.height)
		self.setFixedSize(self.width, self.height)
		self.center()
		
	def ui_config(self):
		font = QtGui.QFont()
		font.setPointSize(14)

		margin_top = self.margin_size * 3
		titleTipLab = QLabel('ZFJVideoTool-视频下载助手', self)
		titleTipLab.setStyleSheet("color:#009900;")
		titleTipLab.setAlignment(Qt.AlignCenter)
		titleTipLab.setGeometry(QtCore.QRect(self.margin_size, margin_top, self.width - self.margin_size * 2, 30))
		font.setPointSize(18)
		titleTipLab.setFont(font)

		margin_top += 30 + self.margin_size * 3
		titleTipLab = QLabel('下载链接', self)
		titleTipLab.setStyleSheet("color:#003366;")
		titleTipLab.setGeometry(QtCore.QRect(self.margin_size, margin_top, self.width - self.margin_size * 2, 20))
		font.setPointSize(14)
		titleTipLab.setFont(font)

		margin_top += 20 + self.margin_size
		self.urlEdit = QtWidgets.QLineEdit(self)
		self.urlEdit.setGeometry(QtCore.QRect(self.margin_size, margin_top, self.width - self.margin_size * 2, 35))
		self.urlEdit.setFont(font)
		self.urlEdit.setPlaceholderText('请输入下载链接')
		self.urlEdit.setAttribute(QtCore.Qt.WA_MacShowFocusRect, 0)
		self.urlEdit.setStyleSheet("border:1px solid #BFBFBF;border-radius:5px;color:#3399FF;")
		self.urlEdit.setText('https://www.bilibili.com/video/BV1uA411B7H8')

		margin_top += 35 + self.margin_size * 2
		titleTipLab = QLabel('保存地址', self)
		titleTipLab.setStyleSheet(";color:#003366;") #background-color:#dcdcdc
		titleTipLab.setGeometry(QtCore.QRect(self.margin_size, margin_top, self.width - self.margin_size * 2, 20))
		font.setPointSize(14)
		titleTipLab.setFont(font)

		margin_top += 20 + self.margin_size
		self.saveEdit = QtWidgets.QLineEdit(self)
		self.saveEdit.setGeometry(QtCore.QRect(self.margin_size, margin_top, self.width - self.margin_size * 3 - 100, 35))
		self.saveEdit.setFont(font)
		self.saveEdit.setPlaceholderText('请选择保存地址')
		self.saveEdit.setAttribute(QtCore.Qt.WA_MacShowFocusRect, 0)
		self.saveEdit.setStyleSheet("border:1px solid #BFBFBF;border-radius:5px;color:#3399FF;")

		choicePathBtn = QtWidgets.QPushButton(self)
		choicePathBtn.setGeometry(QtCore.QRect(self.width - 100 - self.margin_size, margin_top, 100, 30))
		choicePathBtn.setObjectName("logInBtn")
		choicePathBtn.setText(self.translate("MainWindow", "选择文件夹"))
		choicePathBtn.setStyleSheet("color:#009900;background-color:#efeff3;border:1px solid #efeff3;border-radius:5px;")
		choicePathBtn.clicked.connect(self.choicePathBtnClick)
		choicePathBtn.setFont(font)

		margin_top += 35 + self.margin_size
		tipsLab = QLabel('注意：支持youtube、twitter、腾讯、爱奇艺、优酷、bilibili等大部分主流网站视频下载、图片下载！', self)
		tipsLab.setStyleSheet("color:#909190;")
		tipsLab.setGeometry(QtCore.QRect(self.margin_size, margin_top, self.width - self.margin_size * 2, 35))
		font.setPointSize(12)
		tipsLab.setFont(font)

		margin_top += 35 + self.margin_size * 5
		startDownLoadBtn = QtWidgets.QPushButton(self)
		startDownLoadBtn.setGeometry(QtCore.QRect((self.width - 130)/2, margin_top, 130, 35))
		startDownLoadBtn.setText(self.translate("MainWindow", "开始下载"))
		startDownLoadBtn.setStyleSheet("background-color:#fff;color:#009900;border:1px solid #009900;border-radius:5px;")
		startDownLoadBtn.clicked.connect(self.startDownLoadBtnClick)
		font.setPointSize(16)
		startDownLoadBtn.setFont(font)

		self.tipsLab = QLabel('特别说明：本软件仅供交流学习，严禁使用本软件从事非法用途！', self)
		self.tipsLab.setStyleSheet("color:#CC00FF;")
		self.tipsLab.setGeometry(QtCore.QRect(self.margin_size, self.height - 30, self.width - self.margin_size * 2, 30))
		font.setPointSize(12)
		self.tipsLab.setFont(font)

	def startDownLoadBtnClick(self):
		if self.is_downloading == True:
			self.megBoxInfor('正在下载中...')
			return

		self.tipsLab.setText('--')
		url_file_dir = self.urlEdit.text()
		if len(url_file_dir) == 0:
			self.megBoxInfor('请输入下载链接')
			return

		save_file_dir = self.saveEdit.text()
		if len(save_file_dir) == 0:
			self.megBoxInfor('请选择下载保存路径')
			return

		if os.path.exists(save_file_dir) == False:
			self.megBoxInfor('下载保存路径不存在')
			return

		self.tipsLab.setText('正在下载请耐心等待...')
		# self.down_load_by_url(url_file_dir, save_file_dir)
		threading.Thread(target = self.down_load_by_url, args = (url_file_dir, save_file_dir)).start()

	def choicePathBtnClick(self):
		file_dir = ''
		try:
			file_dir = QFileDialog.getExistingDirectory(self,'open file', '/Users/')
		except Exception as e:
			file_dir = '路径获取失败,请手动复制路径'
		
		self.saveEdit.setText(file_dir)

	def megBoxInfor(self, infor):
		infor = infor.replace(':','')
		QMessageBox.information(self, '提示', infor, QMessageBox.Yes)

	#控制窗口显示在屏幕中心的方法    
	def center(self):
		qr = self.frameGeometry()
		cp = QDesktopWidget().availableGeometry().center()
		qr.moveCenter(cp)
		self.move(qr.topLeft())

	def mousePressEvent(self, e):
		if e.button() == Qt.LeftButton:
			self.m_drag = True
			self.m_DragPosition = e.globalPos() - self.pos()
			e.accept()
			# self.setCursor(QCursor(Qt.OpenHandCursor))

	def mouseReleaseEvent(self, e):
		if e.button() == Qt.LeftButton:
			self.m_drag = False
			# self.setCursor(QCursor(Qt.ArrowCursor))

	def mouseMoveEvent(self, e):
		if Qt.LeftButton and self.m_drag:
			self.move(e.globalPos() - self.m_DragPosition)
			e.accept()

	def down_load_by_url(self, url, save_path):
		self.is_downloading = True

		try:
			sys.argv = ['you-get', '-o', save_path, url]
			you_get.main()
			self.tipsLab.setText('下载完成，请到下载保存路径查看')
		except Exception as error:
			self.tipsLab.setText(str(error))
		
		self.is_downloading = False

if __name__ == '__main__':
	app = QApplication(sys.argv)
	videoView = ZFJVideoView()
	videoView.show()
	sys.exit(app.exec_())


# pyinstaller -F -w -i icon.icns ZFJVideoView.py

# pyinstaller ZFJVideoView.spec














